<?php

namespace app\platform\controller\admin;

use app\BaseController;

use app\beautiful\model\order\BuyOrderModel;
use app\beautiful\model\setting\TableNumberModel;
use app\common\model\AllianceConf;

use app\common\model\ShopAllianceConfModel;
use app\common\model\ShopAllianceModel;
use app\common\model\ShopModel;
use app\pay\AllinPay;
use app\pay\AllinPayv2;
use app\websocket\controller\Websocket;
use app\websocket\controller\WebsocketClient;
use think\Config;
use think\console\command\Make;
use think\Event;

class Shopalliance extends BaseController{


    public function list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $keywords = $this->request->param('keywords');
        $where=[];
        $order['a.id']='desc';
        if(!empty($keywords)){
            $where['shop_name']=$keywords;
        }
        $res_data=ShopAllianceModel::list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function info(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('联盟ID错误');
        }
        $where['id']=$id;
        $res_data=ShopAllianceModel::info($where);
        return $this->success($res_data);
    }
    public function effect(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('联盟ID错误');
        }
        $is_effect=$this->request->param('is_effect',null);
        if($is_effect===null){
            return $this->error('is_effect参数错误');
        }
        $where['id']=$id;
        $res=ShopAllianceModel::up(['is_effect'=>$is_effect],$where);
        return $this->success();
    }
    public function del(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('联盟ID错误');
        }
        $is_delete=1;
        $where['id']=$id;
        $info=ShopAllianceModel::info($where);
        if(empty($info)){
            return $this->error('数据不存在');
        }
        $res=ShopAllianceModel::up(['is_delete'=>$is_delete],$where);
        if(!$res){
            return $this->error('删除失败');
        }
        if($info['parent_shop_id']>0){
            $ShopAllianceModel  = new ShopAllianceModel();
            $parent_shop_id=$info['parent_shop_id'];
            $p_w['shop_id']=$parent_shop_id;
            $p_w['parent_shop_id']=0;
            $p_w['is_delete']=0;
            $ShopAllianceModel->dec($p_w,'shop_num',1);//盟主的联盟商家数-1
        }
        //修改门店状态
        ShopModel::up(['is_alliance'=>0],['shop_id'=>$info['shop_id']]);
        //修改联盟配置状态
        ShopAllianceConfModel::up(['is_delete'=>1],['alliance_id'=>$id]);
        return $this->success();
    }
    public function conf_info(){
        $res_data=AllianceConf::info();
        return $this->success($res_data);
    }
    public function conf_edit(){
        $is_shareholder = $this->request->param('is_shareholder',null);
        $is_member=$this->request->param('is_member',null);
        $is_pushflow=$this->request->param('is_pushflow',null);
        $tuodian_takes=$this->request->param('tuodian_takes',null);
        $flow_takes=$this->request->param('flow_takes',null);
        $shop_num=$this->request->param('shop_num',null);
        $up=[];
        if($is_shareholder!=null) $up['is_shareholder']=$is_shareholder;
        if($is_member!=null) $up['is_member']=$is_member;
        if($is_pushflow!=null) $up['is_pushflow']=$is_pushflow;
        if($tuodian_takes!=null) $up['tuodian_takes']=$tuodian_takes;
        if($flow_takes!=null) $up['flow_takes']=$flow_takes;
        if($shop_num!=null) $up['shop_num']=$shop_num;
        $res_data=AllianceConf::up($up);
        return $this->success($res_data);
    }
    public function edit(){
        $id = $this->request->param('id',0);
        $is_tuodian = $this->request->param('is_tuodian',null);
        $is_flow=$this->request->param('is_flow',null);
        $shop_num=$this->request->param('shop_num',null);
        $tuodian_takes=$this->request->param('tuodian_takes',null);
        $flow_takes=$this->request->param('flow_takes',null);
        if($id==0){
            return $this->error('联盟id错误');
        }
        $ShopAllianceModel= new ShopAllianceModel();
        $all_info=$ShopAllianceModel->where(['id'=>$id])->find();
        if(empty($all_info)) return $this->error('该联盟商家不存在');
        $up=[];
        if($is_tuodian!=null) $up['is_tuodian']=$is_tuodian;
        if($is_flow!=null) $up['is_flow']=$is_flow;
        if($shop_num!=null) $up['upper_num']=$shop_num;
        if($tuodian_takes!=null) $up['tuodian_takes']=$tuodian_takes;
        if($flow_takes!=null) $up['flow_takes']=$flow_takes;
        $where['id']=$id;
        $res_data=ShopAllianceModel::up($up,$where);
        return $this->success($res_data);
    }
    public function add(){
        $shop_id = $this->request->param('shop_id',0);
        $pay_type = $this->request->param('pay_type',3);
        $parent_shop_id=$this->request->param('parent_shop_id',0);
        $pay_remark=$this->request->param('pay_remark','');
        $pay_img=$this->request->param('pay_img','');
        $upper_num=$this->request->param('upper_num',0);
        $is_tuodian=$this->request->param('is_tuodian',0);
        $tuodian_takes=$this->request->param('tuodian_takes',0);
        $is_flow=$this->request->param('is_flow',0);
        $flow_takes=$this->request->param('flow_takes',0);
        $start_time=$this->request->param('start_time','');
        $live_num=$this->request->param('live_num',0);
        if($shop_id==0) return $this->error('商铺id参数错误');
        $ShopModel= new ShopModel();
        $shop_info=$ShopModel->find(['shop_id'=>$shop_id]);
        if(empty($shop_info)) return $this->error('商铺不存在');
        $ShopAllianceModel= new ShopAllianceModel();
        $s=[];
        $s[]=['status','<>',2];
        $s[]=['shop_id','=',$shop_id];
        $s[]=['is_delete','=',0];
        $all_info=$ShopAllianceModel->where($s)->find();
        if(!empty($all_info)) return $this->error('该商铺已加入联盟或被其他盟主邀请');
        if($start_time=='') $start_time=date('Y-m-d');
        $all_conf=AllianceConf::info();
        if(!empty($all_conf)){
            if($tuodian_takes==0) $tuodian_takes=$all_conf['tuodian_takes'];
            if($flow_takes==0) $flow_takes=$all_conf['flow_takes'];
            if($upper_num==0) $upper_num=$all_conf['shop_num'];
        }
        $insert=compact('shop_id','pay_type','parent_shop_id','pay_remark','pay_img','upper_num','is_tuodian','tuodian_takes','is_flow','flow_takes','start_time','live_num');
        $insert['add_time']=date('Y-m-d H:i:s');
        $insert['edit_time']=date('Y-m-d H:i:s');
        $insert_id=ShopAllianceModel::insertGetId($insert);
        if($insert_id){
            $alliance_conf=[];
            $alliance_conf['shop_id']=$shop_id;
            $alliance_conf['alliance_id']=$insert_id;
            $alliance_conf['is_shareholder']=$all_conf['is_shareholder'];
            $alliance_conf['is_member']=$all_conf['is_member'];
            $alliance_conf['is_pushflow']=$all_conf['is_pushflow'];
            $alliance_conf['add_time']=date('Y-m-d H:i:s');
            $alliance_conf['edit_time']=date('Y-m-d H:i:s');
            ShopAllianceConfModel::insert($alliance_conf);
            if($insert['parent_shop_id']>0){
                $ShopAllianceModel  = new ShopAllianceModel();
                $parent_shop_id=$insert['parent_shop_id'];
                $p_w['shop_id']=$parent_shop_id;
                $p_w['parent_shop_id']=0;
                $p_w['is_delete']=0;
                $ShopAllianceModel->inc($p_w,'shop_num',1);//盟主的联盟商家数+1
            }
            //修改门店状态
            ShopModel::up(['is_alliance'=>1],['shop_id'=>$shop_id]);
        }
        return $this->success($insert_id);
    }
    public function shop_list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $name = $this->request->param('name');
        $shop_id = $this->request->param('shop_id');
        $where=[];
        $order['a.add_time']='desc';
        if(!empty($name)){
            $where['shop_name']=$name;
        }
        if(!empty($shop_id)){
            $where['shop_id']=$shop_id;
        }
        $res_data=ShopModel::shop_list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function test(){
        $path = $this->request->param('path');
        $res_data=getcsvdata($path);
        var_dump($res_data);exit();
        $filename=date('Ymd').'message.txt';
//        checklog('message',$filename,json_encode(123));
        $authcode = $this->request->param('authcode');
//        $data = $this->request->post();
        $allinpay = new AllinPayv2();
        $res=$allinpay->create('202309271009202452',0.01,$authcode,'测试','/api/pay/notify/');
//        $res=$allinpay->productinfo('202309271009202452',0.01,$authcode,'测试','/api/pay/notify/');
        var_dump($res);exit();
//        $data['content']=$content;
//        $data['uid']=$client_id;
        $client = stream_socket_client('tcp://127.0.0.1:9002', $errno, $errmsg, 1);
// 推送的数据，包含uid字段，表示是给这个uid推送
        $data_sock = array('uid'=>$client_id, 'type'=>'update');
// 发送数据，Text协议需要在数据末尾加上换行符
        $res=fwrite($client, json_encode($data)."\n");
        var_dump($res);exit();
        $where=[];
        $order['a.add_time']='desc';
//        $res_data=ShopModel::shop_list($where,$order,1,10);
//        var_dump($res_data);exit();
       $data['sbx_id']='S01202302230276';
       $data['msg']='支付宝收款12元';
//        $res_data=sendvoice($data);
//        $shop=new ShopAllianceModel();
//        $res=$shop->n_inc(['id'=>17],'shop_num',1);
//        $res=$shop->inc(['id'=>17],'shop_num',1);
//        var_dump($res);
//        memberlock(24,2);
    }

}

